package infocluster.ui.taskpane;

import infocluster.engine.ExcelProcessEngine;
import infocluster.prototype.ExcelProcessTask;
import infocluster.task.ReshapeProcessTask;
import infocluster.ui.taskpane.prototype.TaskPane;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * @author 08302010017 Xi Qi
 *
 */
public class ReshapePane extends TaskPane {

	private static final String DEFAULT_DEST_DIR = "未指定目录";
	private static final String DEFAULT_SOURCE_FILE = "未选择源文件";
	
	private static String DEFAULT_DEST_NAME = "HJBB_Jan_reshaped_offerlist.xls";
	private Text fileDestNameText = null;

	/**
	 * @param arg0
	 * @param arg1
	 */
	public ReshapePane(final Shell shell, final Composite comp, int arg1, final ExcelProcessEngine ENGINE) {
		super(shell, comp, arg1, ENGINE);
		
		ExcelProcessTask task = new ReshapeProcessTask();
		try {
			ENGINE.appendTask(task);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Button buttonSelectFile = buildButton(this, 0, 0, 80, 30, "选择文件",null);
		Button buttonDest = buildButton(this, 0, 40, 80, 30, "目标文件",null);
		
		final Text fileNameText = buildText(this, 90, 0, 360, 30, DEFAULT_SOURCE_FILE, false);		
		final Text fileDestDirText = buildText(this, 90, 40, 250, 30, DEFAULT_DEST_DIR, false);		
		fileDestNameText = buildText(this, 350, 40, 100, 30, DEFAULT_DEST_NAME, true);
		setMonth(ENGINE.getMonth());
		
		buttonSelectFile.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				FileDialog fileDialog = new FileDialog(shell, SWT.SINGLE);

				//fileDialog.setFilterPath(fileFilterPath);

				fileDialog.setFilterExtensions(new String[]{"*.xls", "*.xlsx"});
				fileDialog.setFilterNames(new String[]{ "Excel 2003 or lower", "Excel 2007 or higher"});

				String firstFile = fileDialog.open();

				if(firstFile != null) {
					//fileFilterPath = fileDialog.getFilterPath();
					String[] selectedFiles = fileDialog.getFileNames();
					StringBuffer sb = new StringBuffer(fileDialog.getFilterPath() + "\\");
					for(int i=0; i<selectedFiles.length; i++) {
						sb.append(selectedFiles[i] );
					}
					source.add(sb.toString());
					fileNameText.setText(source.get(0));
				}
			}
		});
		
		buttonDest.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				DirectoryDialog directoryDialog = new DirectoryDialog(shell);
		        
		        directoryDialog.setMessage("请选择路径");
		        
		        String dir = directoryDialog.open();
		        if(dir != null) {
		        	destDir = dir;
		        	fileDestDirText.setText(destDir);
		        }
			}
		});
		
		buttonBegin.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				try {
					//System.out.println(source.size());
					ENGINE.getTask(0).reset();
					if(source.size() < 1){
						showMessage(ReshapePane.this, "无源文件", "请选择源文件");
						return;
					}
					
					if(destDir == null){
						showMessage(ReshapePane.this, "无目标文件夹", "请选择目标文件夹");
						return;
					}
					
					if(fileDestNameText.getText().equals("")){
						showMessage(ReshapePane.this, "无目标文件名", "请输入目标文件名");
						return;
					}
					
					ENGINE.getTask(0).addSource(source.get(0));
					destName.add(fileDestNameText.getText());
					String dest = destDir + "\\" + destName.get(0);
					ENGINE.getTask(0).addDest(dest);
					if(ENGINE.process()){
						showMessage(ReshapePane.this, "清理成功！", "目标文件为：\n"+dest);
						fileNameText.setText(DEFAULT_SOURCE_FILE);
						fileDestDirText.setText(DEFAULT_DEST_DIR);
						fileDestNameText.setText(DEFAULT_DEST_NAME);
						source.clear();
						destDir = null;
						destName.clear();
					}else{
						showMessage(ReshapePane.this, "清理失败！", "请再次尝试。\n");
					}
				} catch (ArrayIndexOutOfBoundsException e) {
					e.printStackTrace();
				} catch (FileNotFoundException e) {
					showMessage(ReshapePane.this, "失败", "同名文件存在，请重新起名或删除原文件");
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
	}

	/* (non-Javadoc)
	 * @see infocluster.ui.taskpane.prototype.TaskPane#setMonth(java.lang.String)
	 */
	@Override
	public void setMonth(String month) {
		// TODO Auto-generated method stub
		String[] temp = DEFAULT_DEST_NAME.split("_");
		DEFAULT_DEST_NAME = temp[0]+"_"+month+"_reshaped_offerlist.xls";
		fileDestNameText.setText(DEFAULT_DEST_NAME);
	}

}
